package base.ebay.delayqueuetest;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayTask implements Delayed {

    //延时任务名称
    private String taskName;
    //延迟时间，单位毫秒
    private long delayTime;
    //认为到期时间，单位毫秒（当前时间+延迟时间）
    private long expiredTime;

    public DelayTask(String taskName, long delayTime) {
        this.taskName = taskName;
        this.delayTime = delayTime;
        expiredTime = System.currentTimeMillis() + delayTime;
    }

    public String getTaskName() {
        return this.taskName;
    }
    //还有多长时间过期
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expiredTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }
}
























































